import {Injectable} from '@angular/core';
import {CanActivate} from '@angular/router';

import {TokenService} from './token.service';
import {NzMessageService} from 'ng-zorro-antd';
import {RouterService} from './router.service';


@Injectable()
export class AuthService implements CanActivate {

    path: string;
    private event = null;

    constructor(
        private tokenService: TokenService,
        private message: NzMessageService,
        private routerService: RouterService
    ) {
    }

    canActivate(routerInfo) {
        const access_token = this.tokenService.get();
        if (access_token) {
            return true;
        } else {
            if (this.event) {
                clearTimeout(this.event);
            }
            this.message.warning('您的登录已过期,请重新登录');
            this.event = setTimeout(() => {
                this.routerService.navigate('/login');
            }, 1000);
            return false;
        }
    }
}
